package 临时软件包.ui;

import com.weiawei.Dialog.BackDialog;
import com.weiawei.Dialog.ExitDialog;
import com.weiawei.Model.TimeThread;
import com.weiawei.ui.InitialMenu;
import com.weiawei.ui.MainBoard;

import javax.swing.*;
import java.awt.event.ActionEvent;

public class  PVEMainBoard extends MainBoard {

    //游戏难度
    private final int level;
    //界面按钮
    protected JButton buttonStart;
    protected JButton buttonBack;
    protected JButton buttonExit;
    // 游戏界面实例
    private PVEChessBorad chessBorad;
    // 返回对话框
    private BackDialog backDialog;

    /**
     * 初始化PVEmainBoard
     *
     * @param level
     */
    public PVEMainBoard(int level) {
        super();
        this.initBoard();
        this.level = level;
    }

    /**
     * 初始化界面的样式
     * <p>
     * 包括棋盘和开始游戏、悔棋、返回三个按钮
     */
    public void initBoard() {
       // System.out.println("检测到棋盘的创建");
        chessBorad = new PVEChessBorad(this);
        buttonStart = new JButton("开始游戏");
        buttonBack = new JButton("悔    棋");
        buttonExit = new JButton("返    回");
        backDialog = new BackDialog(this, "返回菜单", true);

        // 设置棋盘
        // 设置棋盘
        chessBorad.setClickable(CAN_NOT_CLICK);
        chessBorad.setBounds(0, 30, 600, 600);
        chessBorad.setVisible(true);

        //    设置按钮
        buttonStart.setBounds(600, 400, 200, 50);
        buttonBack.setBounds(600, 460, 200, 50);
        buttonExit.setBounds(600, 520, 200, 50);
        setButtonStyle(buttonStart);
        setButtonStyle(buttonBack);
        setButtonStyle(buttonExit);

        //    添加组件
        this.add(chessBorad);
        this.add(buttonStart);
        this.add(buttonBack);
        this.add(buttonExit);

    }

    // 获取游戏难度
    public int getLevel() {
        return level;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == super.getCloseButton()){
            exitDialog.setVisible(true);
        if(exitDialog.getMessage() == ExitDialog.exit){
            System.exit(0);
         }
        }else if (e.getSource() == super.getSmallButton()) {
            setExtendedState(ICONIFIED);
        }else if (e.getSource() == buttonStart) {
            System.out.println("检测到启动开始");
            buttonStart.setEnabled(false);  // 当点击开始游戏时；更改按钮样式
            buttonStart.setText("正在游戏");
            //设置选棋的颜色
            chessBorad.setRole();
            // 计时器开启
            timeThread = new TimeThread(timeCountLabel);
            timeThread.start();
            // 设置棋盘的状态 开始点击
            chessBorad.setClickable(CAN_CLICK);
            chessBorad.setResult(ChessBoard.GAME_START);
        }else if(e.getSource() == buttonBack){
            chessBorad.backStep();
        } else if (e.getSource() == buttonExit) {
            backDialog.setVisible(true);
        if(backDialog.getMessage() == BackDialog.back){
            this.dispose();
            new InitialMenu();
        }
     }
    }
}

